// pages/updataAddress/updataAddress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectoraddress:'请选择',
    info:{},
    warn1:false,
    warn2:false,
    warn3:false,
    warn4:false,
    address:[],
    token:""
    
  },
  //按钮改变事件
  switchChange(e){
        console.log(e.detail.value)
    // this.setData({
    //     isDefault:e.detail.value
    // })
    let that=this
    if(e.detail.value){
      
    wx.request({
      url: 'http://api_dev.wanxikeji.cn/api/userAddressDfault',
      data:{
        token:that.data.token,
        id:that.data.info.address_id,
      },success:res=>{
        console.log(res)
        // wx.navigateBack({
        //   delta: 1
        // })
      }

    })
    }
},

  getname(e){
   this.data.info.name=e.detail.value
  },
  getphone(e){
    this.data.info.phone=e.detail.value
  },
  getdetailaddress(e){
    this.data.info.detailed=e.detail.value
  },
//删除地址
  deladdress(){
    let that=this
    wx.request({
      url: 'http://api_dev.wanxikeji.cn/api/userAddressDelete',
      
      data:{
        token:that.data.token,
        id:that.data.info.address_id,
      },
      success:res=>{
        console.log(res)
        wx.navigateBack({
          delta: 1
        })
      }
      
    })
  },
  addresschecked(e){
    this.setData({address:e.detail.value})
    this.data.info.procince=e.detail.value[0]
    this.data.info.city=e.detail.value[1]
    this.data.info.area=e.detail.value[2]
    let address=e.detail.value[0]+e.detail.value[1]+e.detail.value[2]
    this.setData({selectoraddress:address})
  },
  updateAddress(){//提交修改
    let that=this
    let isok1=true//符合规范
    let isok2=true//符合规范
    let isok3=true//符合规范
    let isok4=true//符合规范

    if(this.data.info.name.replace(/\s+/g,"")==""){
      isok1=false
      console.log(1)
    }
    //校验手机号码
    if(!/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(this.data.info.phone)){
      isok2=false
      console.log(2)
    }
    //校验所在地区
    if(this.data.address.length==0){
      isok3=false
      console.log(3)
    }
    //校验详细地址
    if(this.data.info.detailed.replace(/\s+/g,"")==""){
      isok4=false
      console.log(4)
    }

    if(isok1&&isok2&&isok3&&isok4){
      wx.request({
        url: 'http://api_dev.wanxikeji.cn/api/userAddressAddModify',
        data:{
          token:that.data.token,
          phone:that.data.info.phone,
          procince:that.data.info.procince,
          city:that.data.info.city,
          area:that.data.info.area,
          name:that.data.info.name,
          detailed:that.data.info.detailed,
          address_id:that.data.info.address_id,
        },
        success:res=>{
          console.log(res)
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }
    else if(isok1==false){
      that.setData({warn1:true})
    }
    else if(isok2==false){
      that.setData({warn2:true})
    }
    else if(isok3==false){
      that.setData({warn3:true})
    }
    else if(isok4==false){
      that.setData({warn4:true})
    }

  },
  focus(){
    this.setData({
      warn1:false,
      warn2:false,
      warn3:false,
      warn4:false,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    let that=this
    wx.getStorage({
      key: 'token',
      success:res=>{
       that.setData({token:res.data})
      }
    })
    wx.getStorage({//数据回写到页面
      key: 'updateaddress',
      success:res=>{
        let info=JSON.parse(res.data)
        that.setData({info:info})
        let address =info.procince+info.city+info.area
        that.setData({selectoraddress:address})
        let address2=[]
        address2.push(info.procince)
        address2.push(info.city)
        address2.push(info.area)
        that.setData({address:address2})
      }

      
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})